package cn.sciento.boot.api.customize.compile;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URI;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileObject;

public class CustomJavaFileObject implements JavaFileObject {
  private final String binaryName;
  
  private final URI uri;
  
  private final String name;
  
  public CustomJavaFileObject(String binaryName, URI uri) {
    this.uri = uri;
    this.binaryName = binaryName;
    this.name = (uri.getPath() == null) ? uri.getSchemeSpecificPart() : uri.getPath();
  }
  
  public URI toUri() {
    return this.uri;
  }
  
  public InputStream openInputStream() throws IOException {
    return this.uri.toURL().openStream();
  }
  
  public OutputStream openOutputStream() {
    throw new UnsupportedOperationException();
  }
  
  public String getName() {
    return this.name;
  }
  
  public Reader openReader(boolean ignoreEncodingErrors) {
    throw new UnsupportedOperationException();
  }
  
  public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    throw new UnsupportedOperationException();
  }
  
  public Writer openWriter() throws IOException {
    throw new UnsupportedOperationException();
  }
  
  public long getLastModified() {
    return 0L;
  }
  
  public boolean delete() {
    throw new UnsupportedOperationException();
  }
  
  public Kind getKind() {
    return Kind.CLASS;
  }
  
  public boolean isNameCompatible(String simpleName, Kind kind) {
    String baseName = simpleName + kind.extension;
    return (kind.equals(getKind()) && (baseName
      .equals(getName()) || 
      getName().endsWith("/" + baseName)));
  }
  
  public NestingKind getNestingKind() {
    throw new UnsupportedOperationException();
  }
  
  public Modifier getAccessLevel() {
    throw new UnsupportedOperationException();
  }
  
  public String binaryName() {
    return this.binaryName;
  }
  
  public String toString() {
    return getClass().getName() + "[" + toUri() + "]";
  }
}
